function ret = binaryToArray(data, count)
    % BINARYTOARRAY  convert 0-1 sequence to string.
    % RET = BINARYTOARRAY(DATA, COUNT, RANDSTREAM) converts 0-1 sequence in DATA(1xM)
    % into string, the result are stored in RET, where COUNT tells the
    % function a maximum number of bits to convert. 
    %
    % NOTE: The length field in the message may be ignored if (COUNT-16)/8 
    % is smaller. See help ARRAYTOBINARY for more details.
    
    sizeb = floor(size(data,1)*size(data,2)/8);
    a = uint8(zeros(1, sizeb));
    for index=1:sizeb
        for j=1:8
            i = index*8-8+j;
            a(1,index) = bitset(a(1,index), 9-j, data(i));
        end
    end
    total_len = double(a(1,1))*256+double(a(1,2));
    if total_len*8+16 > count
        len = floor((count-16)/8);
        disp(sprintf('WARNING: wrong value of len = %d, fix it.',total_len));
    else
        len = total_len;
    end
    disp(sprintf('msglen = %dB (%db)',len, 16+len*8));
    ret = char(a(1,3:2+len));
end
